/**
* @project admin-go-api
* @name captcha
* @descriptions 验证码服务
* @author 寒锋
* @date 2023-12-13 16:29:17
 */

package service

import (
	"admin-go-api/common/util"
	"github.com/mojocn/base64Captcha"
	"image/color"
)

var store = util.RedisStore{}

// CaptchaMake 生成验证码
func CaptchaMake() (id, b64s string) {
	// 配置验证码参数
	driverString := base64Captcha.DriverString{
		Height:          60,
		Width:           200,
		NoiseCount:      0,
		ShowLineOptions: 2 | 4,
		Length:          6,
		Source:          "1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM",
		BgColor: &color.RGBA{
			R: 3,
			G: 102,
			B: 214,
			A: 125,
		},
		Fonts: []string{"wqy-microhei.ttc"},
	}
	// ConvertFonts 按名称加载字体
	driver := driverString.ConvertFonts()
	// 创建验证码
	captcha := base64Captcha.NewCaptcha(driver, store)
	// Generate 生成随机 id、base64 图像字符串
	lid, lb64s, _ := captcha.Generate()
	return lid, lb64s
}

// CaptchaVerify 验证captcha是否正确
func CaptchaVerify(id string, captcha string) bool {
	if store.Verify(id, captcha, false) {
		return true
	}
	return false
}
